/*
 * Copyright (c) Created by T C L on 2019.7.9.
 */

package cn.mumzone.pos.ui.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import butterknife.BindView;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.SalesTimesCard;
import cn.mumzone.pos.presenter.SaleCardPresenter;
import cn.mumzone.pos.presenter.contract.SaleCardContract;
import cn.mumzone.pos.ui.adapter.ADA_SalesTimesCard;
import cn.mumzone.pos.ui.widgets.others.StoreHouseHeader;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnLoadMoreListener;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;

public class FRA_SaleCard extends BaseFragment<SaleCardPresenter> implements SaleCardContract.View {
    @BindView(R.id.rlv_cards)
    RecyclerView rlvCards;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.no_times_card_layout)
    LinearLayout noTimesCardLayout;

    private int pageNum = 1;
    private ADA_SalesTimesCard salesTimesCardAdapter;

    @Override
    protected void onUserVisible() {
        smartRefreshLayout.autoRefresh();
    }

    @Override
    protected void initEventAndData() {
        rlvCards.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        smartRefreshLayout.autoRefresh();

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                mPresenter.getSaleTimeCardList(String.valueOf(pageNum), StringUtils.get(AppConstants.PAGE_SIZE), false);
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                mPresenter.getSaleTimeCardList(String.valueOf(pageNum), StringUtils.get(AppConstants.PAGE_SIZE), true);
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_sale_card;
    }

    @Override
    public void setSalesTimesCardData(PageListResponse<SalesTimesCard> timesCard) {
        if (timesCard == null || timesCard.getList() == null || timesCard.getList().isEmpty()) {
            noTimesCardLayout.setVisibility(View.VISIBLE);
            smartRefreshLayout.setVisibility(View.GONE);
        } else {
            noTimesCardLayout.setVisibility(View.GONE);
            smartRefreshLayout.setVisibility(View.VISIBLE);

            salesTimesCardAdapter = new ADA_SalesTimesCard(getActivity(), "");
            rlvCards.setAdapter(salesTimesCardAdapter);
            salesTimesCardAdapter.setDataList(timesCard.getList());
        }
    }

    @Override
    public void appendSalesTimesCardData(PageListResponse<SalesTimesCard> timesCard) {
        salesTimesCardAdapter.update(timesCard.getList());
    }

    @Override
    public void dismissLoading() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    @Override
    public void onPaySuccess() {

    }

    @Override
    protected View getTargetView() {
        return smartRefreshLayout;
    }

    @Override
    protected boolean hasViewPermission() {
        return AppConstants.TEMP_USER_PERMISSION != null && AppConstants.TEMP_USER_PERMISSION.isSellCard();
    }
}
